home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / mach / hurd / __ioctl.c < prev    next >
C/C++ Source or Header  |  1994-06-05  |  7KB  |  239 lines

  1. /* Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <sys/ioctl.h>
  22. #include <hurd.h>
  23. #include <hurd/fd.h>
  24. #include <stdarg.h>
  25. #include <mach/notify.h>
  26. #include <assert.h>
  27.  
  28. /* Symbol set of ioctl handler lists.  If there are user-registered
  29.    handlers, one of these lists will contain them.  The other lists are
  30.    handlers built into the library.  The definition of the set comes from
  31.    hurdioctl.c.  */
  32. extern const struct
  33.   {
  34.     size_t n;
  35.     struct ioctl_handler *v[0];
  36.   } _hurd_ioctl_handler_lists;
  37.  
  38.  
  39. /* Perform the I/O control operation specified by REQUEST on FD.
  40.    The actual type and use of ARG and the return value depend on REQUEST.  */
  41. int
  42. DEFUN(__ioctl, (fd, request),
  43.       int fd AND unsigned long int request DOTS)
  44. {
  45.   /* Map individual type fields to Mach IPC types.  */
  46.   static const int mach_types[] =
  47.     { MACH_MSG_TYPE_CHAR, MACH_MSG_TYPE_INTEGER_16, MACH_MSG_TYPE_INTEGER_32,
  48.       -1 };
  49.  
  50.   /* Extract the type information encoded in the request.  */
  51.   unsigned int type = _IOC_TYPE (request);
  52.  
  53.   /* Message buffer.  */
  54.   struct
  55.     {
  56.       mig_reply_header_t header;
  57.       char data[_IOT_COUNT0 (type) * (_IOT_TYPE0 (request) << 1) +
  58.         _IOT_COUNT1 (type) * (_IOT_TYPE1 (request) << 1) +
  59.         _IOT_COUNT2 (type) * (_IOT_TYPE2 (request) << 1)];
  60.     } msg;
  61.   mach_msg_header_t *const m = &msg.header.Head;
  62.   mach_msg_type_t *t = (mach_msg_type_t *) msg.data;
  63.   mach_msg_id_t msgid;
  64.   unsigned int reply_size;
  65.  
  66.   void *arg;
  67.  
  68.   error_t err;
  69.  
  70. #define io2mach_type(count, type) \
  71.   ((mach_msg_type_t) { mach_types[type], (type << 1) * 8, count, 1, 0, 0 })
  72.  
  73.   /* Pack an argument into the message buffer.  */
  74.   inline void in (unsigned int count, enum __ioctl_datum type)
  75.     {
  76.       if (count > 0)
  77.     {
  78.       void *const p = &t[1];
  79.       const size_t len = count * ((unsigned int) type << 1);
  80.       *t = io2mach_type (count, type);
  81.       memcpy (p, arg, len);
  82.       arg += len;
  83.       t = p + ((len + sizeof (*t) - 1) / sizeof (*t) * sizeof (*t));
  84.     }
  85.     }
  86.  
  87.   /* Unpack the message buffer into the argument location.  */
  88.   inline int out (unsigned int count, unsigned int type,
  89.           void *store, void **update)
  90.     {
  91.       if (count > 0)
  92.     {
  93.       const size_t len = count * (type << 1);
  94.       union { mach_msg_type_t t; int i; } ipctype;
  95.       ipctype.t = io2mach_type (count, type);
  96.       if (*(int *) t != ipctype.i)
  97.         return 1;
  98.       ++t;
  99.       memcpy (store, t, len);
  100.       if (update != NULL)
  101.         *update += len;
  102.       t = (void *) t + ((len + sizeof (*t) - 1) / sizeof (*t) * sizeof *t);
  103.     }
  104.       return 0;
  105.     }
  106.  
  107.   va_list ap;
  108.  
  109.   va_start (ap, request);
  110.   arg = va_arg (ap, void *);
  111.   va_end (ap);
  112.  
  113.   {
  114.     /* Check for a registered handler for REQUEST.  */
  115.  
  116.     size_t i;
  117.     const struct ioctl_handler *h;
  118.  
  119.     for (i = 0; i < _hurd_ioctl_handler_lists.n; ++i)
  120.       for (h = _hurd_ioctl_handler_lists.v[i]; h != NULL; h = h->next)
  121.     if (request >= h->first_request && request <= h->last_request)
  122.       /* This handler groks REQUEST.  Se lo puntamonos.  */
  123.       return (*h->handler) (fd, request, arg);
  124.   }
  125.  
  126.   /* Compute the Mach message ID for the RPC from the group and command
  127.      parts of the ioctl request.  */
  128.   msgid = (100000 +
  129.        ((_IOC_GROUP (request) - 'f') * 4000) + _IOC_COMMAND (request));
  130.  
  131.   if (_IOC_INOUT (request) & IOC_IN)
  132.     {
  133.       /* Pack the argument data.  */
  134.       in (_IOT_COUNT0 (type), _IOT_TYPE0 (type));
  135.       in (_IOT_COUNT1 (type), _IOT_TYPE1 (type));
  136.       in (_IOT_COUNT2 (type), _IOT_TYPE2 (type));
  137.     }
  138.  
  139.   /* Compute the expected size of the reply.  There is a standard header
  140.      consisting of the message header and the reply code.  Then, for out
  141.      and in/out ioctls, there come the data with their type headers.  */
  142.   reply_size = sizeof (mig_reply_header_t);
  143.  
  144.   if (_IOC_INOUT (request) & IOC_OUT)
  145.     {
  146.       inline void figure_reply (unsigned int count, enum __ioctl_datum type)
  147.     {
  148.       if (count > 0)
  149.         {
  150.           /* Add the size of the type and data.  */
  151.           reply_size += sizeof (mach_msg_type_t) + (type << 1) * count;
  152.           /* Align it to word size.  */
  153.           reply_size += sizeof (mach_msg_type_t) - 1;
  154.           reply_size &= ~(sizeof (mach_msg_type_t) - 1);
  155.         }
  156.     }
  157.       figure_reply (_IOT_COUNT0 (request), _IOT_TYPE0 (request));
  158.       figure_reply (_IOT_COUNT1 (request), _IOT_TYPE1 (request));
  159.       figure_reply (_IOT_COUNT2 (request), _IOT_TYPE2 (request));
  160.     }
  161.  
  162.   /* Send the RPC.  */
  163.  
  164.   err = HURD_DPORT_USE
  165.     (fd,
  166.      ({
  167.        m->msgh_size = (char *) t - (char *) &msg;
  168.        m->msgh_remote_port = port;
  169.        m->msgh_local_port = __mig_get_reply_port ();
  170.        m->msgh_seqno = 0;
  171.        m->msgh_id = msgid;
  172.        m->msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
  173.                       MACH_MSG_TYPE_MAKE_SEND_ONCE);
  174. #ifdef notyet
  175.        HURD_EINTR_RPC (port, __mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG,
  176.                      m->msgh_size, sizeof (msg),
  177.                      m->msgh_local_port,
  178.                      MACH_MSG_TIMEOUT_NONE,
  179.                      MACH_PORT_NULL));
  180. #else
  181.        __mach_msg (m, MACH_SEND_MSG|MACH_RCV_MSG, m->msgh_size, sizeof (msg),
  182.            m->msgh_local_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
  183. #endif       
  184.      }));
  185.  
  186.   switch (err)
  187.     {
  188.     case MACH_MSG_SUCCESS:
  189.       break;
  190.     case MACH_SEND_INVALID_REPLY:
  191.     case MACH_RCV_INVALID_NAME:
  192.       __mig_dealloc_reply_port ();
  193.     default:
  194.       return __hurd_fail (err);
  195.     }
  196.  
  197.   if ((m->msgh_bits & MACH_MSGH_BITS_COMPLEX))
  198.     {
  199.       /* Allow no ports or VM.  */
  200.       __mach_msg_destroy (m);
  201.       /* Want to return a different error below for a different msgid.  */
  202.       if (m->msgh_id == msgid + 100)
  203.     return __hurd_fail (MIG_TYPE_ERROR);
  204.     }
  205.  
  206.   if (m->msgh_id != msgid + 100)
  207.     return __hurd_fail (m->msgh_id == MACH_NOTIFY_SEND_ONCE ?
  208.             MIG_SERVER_DIED : MIG_REPLY_MISMATCH);
  209.  
  210.   if (m->msgh_size != reply_size &&
  211.       m->msgh_size != sizeof (mig_reply_header_t))
  212.     return __hurd_fail (MIG_TYPE_ERROR);
  213.  
  214.   if (*(int *) &msg.header.RetCodeType !=
  215.       ((union { mach_msg_type_t t; int i; })
  216.        { t: io2mach_type (1, _IOTS (sizeof msg.header.RetCode)) }).i)
  217.     return __hurd_fail (MIG_TYPE_ERROR);
  218.   err = msg.header.RetCode;
  219.  
  220.   t = (mach_msg_type_t *) msg.data;
  221.   switch (err)
  222.     {
  223.     case 0:
  224.       if (m->msgh_size != reply_size ||
  225.       out (_IOT_COUNT0 (type), _IOT_TYPE0 (type), arg, &arg) ||
  226.       out (_IOT_COUNT1 (type), _IOT_TYPE1 (type), arg, &arg) ||
  227.       out (_IOT_COUNT2 (type), _IOT_TYPE2 (type), arg, &arg))
  228.     return __hurd_fail (MIG_TYPE_ERROR);
  229.       return 0;
  230.  
  231.     case MIG_BAD_ID:
  232.     case EOPNOTSUPP:
  233.       /* The server didn't understand the RPC.  */
  234.       err = ENOTTY;
  235.     default:
  236.       return __hurd_fail (err);
  237.     }
  238. }
  239.